//
//  WWWritePasswordViewController.m
//  WeiWeiHaircut
//
//  Created by 超群 on 15/10/14.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "WWWritePasswordViewController.h"
#import "WWRegisterCell.h"

@interface WWWritePasswordViewController () <UITableViewDelegate,UITableViewDataSource,QZRequestProtocol>
@property (strong,nonatomic)QZRequest *request;
@end

@implementation WWWritePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self buildTableViewUI];
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidenKeyBoard)];
    [self.view addGestureRecognizer:tap];
    
    self.request = [[QZRequest alloc]init];
    self.request.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buildTableViewUI
{
    self.tableView.delegate         = self;
    self.tableView.dataSource       = self;
    self.tableView.backgroundColor  = [UIColor groupTableViewBackgroundColor];

    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WWRegisterCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([WWRegisterCell class])];
}

- (void)hidenKeyBoard
{
    [self.view endEditing:YES];
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

#pragma mark - tableView datasource
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WWRegisterCell * cell  = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WWRegisterCell class]) forIndexPath:indexPath];
    [cell.textField setTag:indexPath.row+1];
    cell.textField.secureTextEntry = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"密    码:";
        cell.textField.secureTextEntry = YES;
    }
    if (indexPath.row == 1) {
        cell.titleLabel.text = @"确认密码:";
    }

    return cell;
}
- (IBAction)submitTap:(UIButton *)sender {
    UITextField* textfield1 = (UITextField*)[self.view viewWithTag:1];
    UITextField* textfield2 = (UITextField*)[self.view viewWithTag:2];
    if (![self checkTextfieldText:textfield1]) {
        [[iToast makeText:@"密码不能为空"] show];
        return;
    }
    if (![self checkTextfieldText:textfield2]) {
        [[iToast makeText:@"确认密码不能为空"] show];
        return;
    }
    if(![textfield1.text isEqualToString:textfield2.text]){
        [[iToast makeText:@"两次密码不一致"] show];
        return;
    }
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",newPublicURL,@"User.GetSetPwd"];
    NSDictionary *dic = @{@"user_id":_userName,
                          @"password" :[textfield1.text delSpace],
                          @"password2" :[textfield2.text delSpace]};
    
    [self.request QZRequest_POST:urlStr parameters:dic tagNSString:@"User.GetSetPwd" stopRequest:NO isSerializer:NO isCache:NO];
}
#pragma  mark QZRequestProtocol
-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag
{
    NSLog(@"value = %@",value);
   
    if ([tag isEqualToString:@"User.GetSetPwd"]) {
        if ([value[@"data"][@"status"] intValue] == 100) {
            [[iToast makeText:@"密码重置成功!"] show];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            NSString *tt = [NSString stringWithFormat:@"%@",value[@"data"][@"data"][@"info"]];
            [[iToast makeText:tt] show];
        }
    }
}
- (BOOL )checkTextfieldText:(UITextField*)textField
{
    NSString* firstrPassword = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([firstrPassword isEqualToString:@""]) {
        return NO;
    }else
        return YES;
}

@end
